package com.ruoyi.voucher.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.voucher.domain.Scrip;
import com.ruoyi.voucher.domain.VoucherDetail;
import com.ruoyi.voucher.dto.ScripDTO;
import com.ruoyi.voucher.dto.ScripDetailDTO;
import com.ruoyi.voucher.mapper.VoucherDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.voucher.mapper.scripMapper;
import com.ruoyi.voucher.service.IscripService;

/**
 * 凭证主Service业务层处理
 * 
 * @author 孙帆科
 * @date 2025-02-26
 */
@Service
public class scripServiceImpl implements IscripService 
{
    @Autowired
    private scripMapper scripMapper;

    @Autowired
    private VoucherDetailMapper voucherDetailMapper;

    /**
     * 查询凭证主
     * 
     * @param voucherId 凭证主主键
     * @return 凭证主
     */
    @Override
    public Scrip selectscripByVoucherId(Long voucherId)
    {
        return scripMapper.selectscripByVoucherId(voucherId);
    }

    /**
     * 查询凭证主列表
     * 
     * @param scrip 凭证主
     * @return 凭证主
     */
    @Override
    public List<Scrip> selectscripList(Scrip scrip)
    {
        return scripMapper.selectscripList(scrip);
    }

    /**
     * 新增凭证主
     * 
     * @param
     * @return 结果
     */
    @Override
    public int insertscrip(Scrip scrip)
    {
        return scripMapper.insertscrip(scrip);
    }

    /**
     * 修改凭证主
     * 
     * @param scrip 凭证主
     * @return 结果
     */
    @Override
    public int updatescrip(Scrip scrip)
    {
        scrip.setUpdateTime(DateUtils.getNowDate());
        return scripMapper.updatescrip(scrip);
    }

    /**
     * 批量删除凭证主
     * 
     * @param voucherIds 需要删除的凭证主主键
     * @return 结果
     */
    @Override
    public int deletescripByVoucherIds(Long[] voucherIds)
    {
        return scripMapper.deletescripByVoucherIds(voucherIds);
    }

    /**
     * 删除凭证主信息
     * 
     * @param voucherId 凭证主主键
     * @return 结果
     */
    @Override
    public int deletescripByVoucherId(Long voucherId)
    {
        return scripMapper.deletescripByVoucherId(voucherId);
    }

    @Override
    public int insertscripDTO(ScripDTO scripDTO) {
        //创建凭证对象
        Scrip scrip = new Scrip();
        scrip.setVoucherNo(scripDTO.getVoucherNo());
        scrip.setVoucherDate(new Date());
        //把日期转换为年月日
        Date date = new Date();
        scrip.setAccountPeriod(String.valueOf(date));
        scrip.setAttachmentCount(scripDTO.getAttachmentCount());
        //设置默认审核状态为未审核
        scrip.setAuditStatus(0);
        //设置默认作废状态为未作废
        scrip.setIsVoid(1);
//        根据当前用户id查询用户信息
//        Long userId = SecurityUtils.getUserId();
//        new QueryWrapper<>()

        scrip.setWordId(scripDTO.getWordId());
        //获取当前用户名
        String username = SecurityUtils.getUsername();
        scrip.setCreateBy(username);
        //获取当前时间
        scrip.setCreateTime(new Date());
        //获取明细表集合
        List<ScripDetailDTO> scripDetailDTOList = scripDTO.getScripDetailDTOList();
        if(scripDetailDTOList==null){
            throw new RuntimeException("凭证明细不能为空");
        }
        //计算总金额
        Double totalAmount = scripDTO.getTotalAmount();
        if(totalAmount == null){
            totalAmount = 0.0;
            for (ScripDetailDTO scripDetailDTO : scripDetailDTOList) {
                totalAmount += scripDetailDTO.getDebitAmount();
            }
            scripDTO.setTotalAmount(totalAmount);
        }
        else {
            Double getTotalAmount = 0.0;
            for (ScripDetailDTO scripDetailDTO : scripDetailDTOList) {
                getTotalAmount += scripDetailDTO.getDebitAmount();
            }
            //判断总金额是否与明细表总金额一致
            if(!getTotalAmount.equals(scripDTO.getTotalAmount())){
                throw new RuntimeException("凭证明细总金额与凭证总金额不一致");
            }
            scrip.setTotalAmount(scripDTO.getTotalAmount());
        }
        scrip.setAttachmentCount(scripDTO.getAttachmentCount());
        //添加凭证主表
        scripMapper.insertscrip(scrip);
        for (ScripDetailDTO scripDetailDTO : scripDetailDTOList) {
            if(!scripDetailDTO.getDebitAmount().equals(scripDetailDTO.getDebitAmount())){
                throw new RuntimeException("凭证明细借方金额与凭证明细贷方金额不一致");
            }
            if(scripDetailDTO.getCreditAmount()==null && scripDetailDTO.getDebitAmount()==null){
                throw new RuntimeException("凭证明细借方金额与凭证明细贷方金额不能为空");
            }
            if(scripDetailDTO.getCreditAmount()==0 && scripDetailDTO.getDebitAmount()==0){
                throw new RuntimeException("凭证明细借方金额与凭证明细贷方金额不能为0");
            }
            if(scripDetailDTO.getCreditAmount()==0 || scripDetailDTO.getCreditAmount()==null){
                throw new RuntimeException("凭证明细贷方金额不能为0");
            }
            if(scripDetailDTO.getDebitAmount()==0 || scripDetailDTO.getDebitAmount()==null){
                throw new RuntimeException("凭证明细借方金额不能为0");
            }
            VoucherDetail voucherDetail = new VoucherDetail();
            if(scrip.getVoucherId()!=null){
                voucherDetail.setVoucherId(scrip.getVoucherId());
            }
            if(scripDetailDTO.getSubjectId()!=null){
                voucherDetail.setSubjectId(scripDetailDTO.getSubjectId());
            }
            if(scripDetailDTO.getSummaryId()!=null){
                voucherDetail.setSummaryId(scripDetailDTO.getSummaryId());
            }
            voucherDetail.setDebitAmount(scripDetailDTO.getDebitAmount());
            voucherDetail.setCreditAmount(scripDetailDTO.getCreditAmount());
            return voucherDetailMapper.insertVoucherDetail(voucherDetail);
        }
        return 1;
    }
}
